var app = getApp(), fetchApi = require("../../utils/httpPost.js"), common_js = require("../../component/common.js")
import Toast from '../../component/vant/toast/toast';
import Dialog from '../../component/vant/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.getImgUrl(),
    current:0,
    state:0,
    pageSize:10,
    pageIndex:1,
    couponList:[],
    banners:[],
    tabIndex:0,
    hasData:false,
    netWork:true,
    isLoading:true,
    loading:false,
    is_mer:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.onHttpInit()
  },
  reload:function(){
    this.onHttpInit()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/coupon/list', {
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex
    }, function (res) {
      console.log("优惠券列表数据", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex += 1;
          var coupons = res.data.data;
          if(coupons.length > 0){
            for (var index in coupons) {
              that.data.couponList.splice(that.data.couponList.length, 0, coupons[index]);
            }
            that.setData({ 
              hasData: true,
              couponList: that.data.couponList,
            })
          }
        }
      }
      that.setData({ netWork: true, isLoading: false, loading: true})
    },function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, loading: false })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  goMyCoupon(){
    wx.navigateTo({
      url: '/pages/coupon/user/index',
    })
  },
  goBuy(e){
    console.log('goBuy e',e)
    var couponId = e.currentTarget.dataset.item.id;
    var price = e.currentTarget.dataset.item.singlePrice;
    var that = this;
    var message = "需要消耗 "+ price +" 物业金兑换，是否要购买？";
    Dialog.confirm({
      title: '购买优惠券',
      message
    }).then(() => {
      that.onHttpBuy(couponId)
    })
    .catch(() => {
      Dialog.close();
    });
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpBuy: function (coupon_id) {
    var that = this
    fetchApi.httpPost('/coupon/buyCouponByYd', {
      coupon_id: coupon_id,
    }, function (res) {
      console.log("购买优惠券", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
      }else{
        Toast.fail(res.data.msg);
      }
      that.setData({ netWork: true, isLoading: false, loading: true})
    },function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, loading: false })
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.setData({ couponList: []})
    if(this.data.state == 3){
      this.onHttpTicketRecord()
    }else{
      this.onHttpInit()
    }
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    if(this.data.state == 3){
      this.onHttpTicketRecord()
    }else{
      this.onHttpInit()
    }
  },
  onShow:function(){
    var that = this
    var isLogin = app.getStorageSync("isLogin");
    if (isLogin == 0) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } else {
      this.setData({
        is_mer: app.getStorageSync("is_mer")
      })
      this.setData({
        current: 0,
        couponList: [],
        isLoading: true,
        hasData: false,
        pageIndex: 1,
        tabIndex: 0
      });
      if(this.data.state == 3){
        this.onHttpTicketRecord()
      }else{
        this.onHttpInit()
      }
    }
  }
})